﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ImmovablesManager
{       
    
        public enum TypeObject
        {
            Flat, Commercial, Plot
        }

    public class FilterFlat
    {
        public string TypeCard { get; set; }
        public Guid? RegionId { get; set; }
        public double? PriceFrom { get; set; }
        public double? PriceTo { get; set; }
        public double? AreaFrom { get; set; }
        public double? AreaTo { get; set; }
        public Guid? TypeHouseId { get; set; }
        public string Floors { get; set; }
        public int? RoomCount { get; set; }
        
    }
    public class FilterPlot
    {
        public string TypeCard { get; set; }
        public Guid? RegionId { get; set; }
        public double? PriceFrom { get; set; }
        public double? PriceTo { get; set; }
        public double? AreaFrom { get; set; }
        public double? AreaTo { get; set; }
        public bool IsWater { get; set; }
        public bool IsElect { get; set; }
        public bool IsCanal { get; set; }
        public bool IsGas { get; set; }
        public bool? IsCommunicate { get; set; }
    }
    public class FilterCommercial
    {
        public string TypeCard { get; set; }
        public Guid? RegionId { get; set; }
        public double? PriceFrom { get; set; }
        public double? PriceTo { get; set; }
        public double? AreaFrom { get; set; }
        public double? AreaTo { get; set; }
        public int? RoomCount { get; set; }
        public Guid? TypeCommercial { get; set; }
    }
    /// <summary>
    /// Interaction logic for FilterWindow.xaml
    /// </summary>
    public partial class FilterWindow : Window
    {
        private TypeObject typeObject;
        public FilterFlat filterFlat;
        public FilterPlot filterPlot;
        public FilterCommercial filterCommercial;

        public FilterWindow(TypeObject _typeObject)
        {

            typeObject = _typeObject;  

            InitializeComponent();
            FillMain();

            switch (typeObject)
            {
                case  TypeObject.Flat:
                    {
                        FillFlatFields();
                        break;
                    }
                case TypeObject.Commercial:
                    {
                        FillCommercialFields();
                        break;
                    }
                    
                case TypeObject.Plot:
                    {
                        FillPlotFlatFields();
                        break;
                    }
                default:
                    break;
            }


            this.typeObject = typeObject;
        }

       
        #region checkBox

        private void ChkIsRoom_Click(object sender, RoutedEventArgs e)
        {
            VisibilityControl(rooms, (bool)(sender as CheckBox).IsChecked);
        }
        private void ChkIsType_Click(object sender, RoutedEventArgs e)
        {
            VisibilityControl(typeCard, (bool) (sender as CheckBox).IsChecked);
        }
        private void ChkIsTypeComm_Click(object sender, RoutedEventArgs e)
        {
            VisibilityControl(TypeCommercial, (bool)(sender as CheckBox).IsChecked);
        }
        private void ChkIsArea_Click(object sender, RoutedEventArgs e)
        {
            VisibilityDockPanel(AreaStack, (bool)(sender as CheckBox).IsChecked);
        }

        private void ChkIsPrice_Click(object sender, RoutedEventArgs e)
        {
            VisibilityDockPanel(PriceDock, (bool)(sender as CheckBox).IsChecked);
        }

        private void ChkIsTypeHouse_Click(object sender, RoutedEventArgs e)
        {
            VisibilityControl(typeHouse, (bool)(sender as CheckBox).IsChecked);
        }

        private void ChkIsFloors_Click(object sender, RoutedEventArgs e)
        {
            VisibilityControl(floors, (bool)(sender as CheckBox).IsChecked);
        }

        private void ChkIsCommunication_Click(object sender, RoutedEventArgs e)
        {
            VisibilityControl(ChkIsWater, (bool)(sender as CheckBox).IsChecked);
            VisibilityControl(Label1, (bool)(sender as CheckBox).IsChecked);
            VisibilityControl(Label2, (bool)(sender as CheckBox).IsChecked);
            VisibilityControl(Label3, (bool)(sender as CheckBox).IsChecked);
            VisibilityControl(Label4, (bool)(sender as CheckBox).IsChecked);
            VisibilityControl(ChkIsCanaliz, (bool)(sender as CheckBox).IsChecked);
            VisibilityControl(ChkIsElectr, (bool)(sender as CheckBox).IsChecked);
            VisibilityControl(ChkIGas, (bool)(sender as CheckBox).IsChecked); 
        }

        private void ChkRegion_Click(object sender, RoutedEventArgs e)
        {
            VisibilityControl(regionCard, (bool)(sender as CheckBox).IsChecked);
        }

        private void VisibilityControl(System.Windows.Controls.Control control, bool isVisibile)
        {
            if(isVisibile)
            {
                control.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                control.Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void VisibilityDockPanel(DockPanel control, bool isVisibile)
        {
            if (isVisibile)
            {
                control.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                control.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        private void VisibilityStackPanel(StackPanel control, bool isVisibile)
        {
            if (isVisibile)
            {
                control.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                control.Visibility = System.Windows.Visibility.Collapsed;
            }
        }
        #endregion checkBox



        public class RegionsComboBox
        {
            public string RegionName { get; set; }
            public Guid RegionGuid { get; set; }

            public RegionsComboBox(string regionName,
                                 Guid regionGuid)
            {
                RegionName = regionName;
                RegionGuid = regionGuid;
            }
        }

        public class TypeHouseComboBox
        {
            public string TypeHouseName { get; set; }
            public Guid TypeHouseGuid { get; set; }

            public TypeHouseComboBox(string typeHouseName,
                                 Guid typeHouseGuid)
            {
                TypeHouseName = typeHouseName;
                TypeHouseGuid = typeHouseGuid;
            }
        }

        public class CountRoomComboBox
        {
            public string CountRoomName { get; set; }
            public Guid CountRoomGuid { get; set; }

            public CountRoomComboBox(string countRoomName,
                                 Guid countRoomGuid)
            {
                CountRoomName = countRoomName;
                CountRoomGuid = countRoomGuid;
            }
        }

        public class TypeCommercialComboBox
        {
            public string TypeName { get; set; }
            public Guid TypeGuid { get; set; }

            public TypeCommercialComboBox(string typeName,
                                    Guid typeGuid)
            {
                TypeName = typeName;
                TypeGuid = typeGuid;
            }
        }


        private void FillMain()
        {
            var regionsComboBoxCollection = new List<RegionsComboBox>();
            regionsComboBoxCollection.AddRange(AppController.ImmoRepository.GetAllRegions().Select(region =>
                                                                                                   new RegionsComboBox(
                                                                                                       region.RegionName,
                                                                                                       region.RegionId)));

            regionCard.ItemsSource = regionsComboBoxCollection;
            regionCard.SelectedItem = regionsComboBoxCollection[0];


            typeCard.Items.Add(new ComboBoxItem() {Content = "Продам"});
            typeCard.Items.Add(new ComboBoxItem() { Content = "Сдам" });
            typeCard.Items.Add(new ComboBoxItem() { Content = "Куплю" }); 
            typeCard.Items.Add(new ComboBoxItem() { Content = "Сниму" });
            typeCard.SelectedIndex = 0;

            PriceFromTBx.Text = "0";
            PriceToTBx.Text = "0";
            AreaToTBx.Text = "0";
            AreaFromTBx.Text = "0";
        }


        private void FillFlatFields()
        {
            VisibilityStackPanel(FlarPanel, true);

            var typeHouseComboBoxCollection = new List<TypeHouseComboBox>();
            typeHouseComboBoxCollection.AddRange(AppController.ImmoRepository.GetAllHouseMaterial().Select(material =>
                                                                                                   new TypeHouseComboBox(
                                                                                                       material.HouseMaterialName,
                                                                                                       material.HouseMaterialId)));

            typeHouse.ItemsSource = typeHouseComboBoxCollection;
            typeHouse.SelectedItem = typeHouseComboBoxCollection[0];


            floors.Items.Add(new ComboBoxItem() { Content = "<5" });
            floors.Items.Add(new ComboBoxItem() { Content = "5-8" });
            floors.Items.Add(new ComboBoxItem() { Content = "9-16" });
            floors.Items.Add(new ComboBoxItem() { Content = ">16" });
            floors.SelectedIndex = 0;

            rooms.Items.Add(new ComboBoxItem() { Content = "1"});
            rooms.Items.Add(new ComboBoxItem() { Content = "2" });
            rooms.Items.Add(new ComboBoxItem() { Content = "3" });
            rooms.Items.Add(new ComboBoxItem() { Content = "4" });
            rooms.SelectedIndex = 0;

        }
        private void FillPlotFlatFields()
        {
            VisibilityControl(Label22, true);
            VisibilityControl(ChkIsCommunication, true);
            VisibilityStackPanel(PlotPanel, true);

        }

        private void FillCommercialFields()
        {
            VisibilityStackPanel(CommercialPanel, true);
            var typeCommercialComboBoxCollection = new List<TypeCommercialComboBox>();
            typeCommercialComboBoxCollection.AddRange(AppController.ImmoRepository.GetAllTypeEstate().Select(type =>
                                                                                                   new TypeCommercialComboBox(
                                                                                                      type.CommercialRealEstateTypeName,
                                                                                                       type.CommercialRealEstateTypeId)));

            TypeCommercial.ItemsSource = typeCommercialComboBoxCollection;
            TypeCommercial.SelectedItem = typeCommercialComboBoxCollection[0];
        }

        private void BtnOk_click(object sender, RoutedEventArgs e)
        {
            switch (typeObject)
            {
                case TypeObject.Flat:
                    {
                        FilterFlatFields();
                        break;
                    }
                case TypeObject.Commercial:
                    {
                        FilterCommercialFields();
                        break;
                    }

                case TypeObject.Plot:
                    {
                        FilterPlotFields();
                        break;
                    }
                default:
                    break;
            }

            Close();
        }


        private void FilterPlotFields()
        {
            filterPlot = new FilterPlot();

            if ((bool)ChkIsType.IsChecked)
                filterPlot.TypeCard = (typeCard.SelectedItem as ComboBoxItem).Content.ToString();
            if ((bool)ChkIsRegion.IsChecked)
                filterPlot.RegionId = (Guid?)regionCard.SelectedValue;
            if ((bool)ChkIsPrice.IsChecked && PriceFromTBx.Text != null && PriceToTBx.Text != null)
            {
                filterPlot.PriceFrom = double.Parse(PriceFromTBx.Text);
                filterPlot.PriceTo = double.Parse(PriceToTBx.Text);
            }
            if ((bool)ChkIsArea.IsChecked && AreaFromTBx.Text != null && AreaToTBx.Text != null)
            {
                filterPlot.AreaFrom = double.Parse(AreaFromTBx.Text);
                filterPlot.AreaTo = double.Parse(AreaToTBx.Text);
            }
            ///////
            if((bool) ChkIsCommunication.IsChecked)
            {
                filterPlot.IsWater = (bool) ChkIsWater.IsChecked;
                filterPlot.IsElect = (bool) ChkIsElectr.IsChecked;
                filterPlot.IsGas = (bool) ChkIGas.IsChecked;
                filterPlot.IsCanal = (bool) ChkIsCanaliz.IsChecked;
                filterPlot.IsCommunicate = ChkIsCommunication.IsChecked;
            }
            /// 
        }

        private void FilterCommercialFields()
        {
            filterCommercial = new FilterCommercial();
            if ((bool)ChkIsType.IsChecked)
                filterCommercial.TypeCard = (typeCard.SelectedItem as ComboBoxItem).Content.ToString();
            if ((bool)ChkIsRegion.IsChecked)
                filterCommercial.RegionId = (Guid?)regionCard.SelectedValue;
            if ((bool)ChkIsPrice.IsChecked && PriceFromTBx.Text != null && PriceToTBx.Text != null)
            {
                filterCommercial.PriceFrom = double.Parse(PriceFromTBx.Text);
                filterCommercial.PriceTo = double.Parse(PriceToTBx.Text);
            }
            if ((bool)ChkIsArea.IsChecked && AreaFromTBx.Text != null && AreaToTBx.Text != null)
            {
                filterCommercial.AreaFrom = double.Parse(AreaFromTBx.Text);
                filterCommercial.AreaTo = double.Parse(AreaToTBx.Text);
            }
            if ((bool)ChkIsTypeComm.IsChecked)
                filterCommercial.TypeCommercial = (Guid?) TypeCommercial.SelectedValue;
        }

        private void FilterFlatFields()
        {
            filterFlat = new FilterFlat();
            if ((bool)ChkIsType.IsChecked)
               filterFlat.TypeCard = (typeCard.SelectedItem as ComboBoxItem).Content.ToString();
            if ((bool) ChkIsRegion.IsChecked)
                filterFlat.RegionId = (Guid?) regionCard.SelectedValue;
            if ((bool)ChkIsPrice.IsChecked && PriceFromTBx.Text != null && PriceToTBx.Text != null)
            {
                filterFlat.PriceFrom = double.Parse(PriceFromTBx.Text);
                filterFlat.PriceTo = double.Parse(PriceToTBx.Text);
            }
            if ((bool)ChkIsArea.IsChecked && AreaFromTBx.Text != null && AreaToTBx.Text != null)
            {
                filterFlat.AreaFrom = double.Parse(AreaFromTBx.Text);
                filterFlat.AreaTo = double.Parse(AreaToTBx.Text);
            }
            ///////
            if ((bool)ChkIsTypeHouse.IsChecked)
                filterFlat.TypeHouseId = (Guid?) typeHouse.SelectedValue;
            if ((bool)ChkIsFloors.IsChecked )
                filterFlat.Floors = (floors.SelectedItem as ComboBoxItem).Content.ToString();
            if ((bool)ChkIsRoom.IsChecked)
                filterFlat.RoomCount = int.Parse((rooms.SelectedItem as ComboBoxItem).Content.ToString());


        }

        private void BtnCancel_click(object sender, RoutedEventArgs e)
        {
            Close();
        }



    }
}
